home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / src / mactech / volume14_1998 / 14.12.sit / 14.12 / Poor Man's Bryce 3.0 / PictRead.c < prev    next >
Text File  |  1998-08-10  |  4KB  |  154 lines

  1. // PictRead.c (for PMB v3.0)
  2. //
  3. // ©1998 by Kas Thomas. Portions ©1998 by Xplain Corp. and MacTech Magazine.
  4. //
  5. // These routines let our user find a PICT file to use as a texture map.
  6.  
  7. #include <StandardFile.h>
  8. #include <Memory.h>
  9. #include <TextUtils.h>
  10. #include <QDOffscreen.h>
  11.  
  12. #include "QD3D.h"
  13. #include "QD3DStorage.h"
  14.  
  15. #include "PictRead.h"
  16.  
  17.  
  18. // ------------------------ OpenPICTFile() ------------------------
  19. // open a PICT file and read it into a PICT handle
  20. PicHandle OpenPICTFile(
  21.     short     vRefNum, 
  22.     Str255     fName)
  23. {
  24.     short        fRefNum = 0;
  25.     OSErr        err;
  26.     long        curEOF;
  27.     PicHandle    myPic = nil;
  28.     long         count;
  29.     
  30.     
  31.     err = FSOpen(fName, vRefNum, &fRefNum);        // open PICT file 
  32.     if (err) goto bail;
  33.             
  34.     err = GetEOF(fRefNum, &curEOF);                // get size of file     
  35.     if (err) goto bail;
  36.     
  37.     err = SetFPos(fRefNum, fsFromStart, 512L);    // move past header    
  38.     if (err) goto bail;
  39.      
  40.     count = curEOF - 512L;                        // size of data to read
  41.     
  42.     myPic = (PicHandle)NewHandleClear(count);    // allocate PicHandle 
  43.     if ( myPic == nil )
  44.         goto bail;
  45.     
  46.     HLock((Handle)myPic);                         // lock the handle
  47.                             
  48.     err = FSRead(fRefNum, &count,(Ptr) *myPic);    // read the PICT info
  49.     
  50.     HUnlock((Handle)myPic);                        // unlock handle    
  51.     
  52. bail:
  53.     IOErr( err );                                // report any errors    
  54.     
  55.     if (fRefNum)                                // if file was open, close it
  56.         FSClose( fRefNum );
  57.         
  58.     return (myPic);                                // return the PicHandle
  59. }
  60.  
  61. // -------------------------- LoadMapPICT() ---------------------------
  62. // This is where our PICT texture gets made into a StoragePixmap so
  63. // QD3D can use it. Remember, QD3D is cross-platform and can't use
  64. // regular PixMaps!
  65. //
  66. // Version 3.0: Makes all textures 16-bit instead of 32, for speed.
  67.  
  68. short LoadMapPICT(
  69.     PicHandle             pict,
  70.     unsigned long         mapSizeX,
  71.     unsigned long         mapSizeY,
  72.     TQ3StoragePixmap     *bMap)
  73. {
  74.     unsigned long            pictMapAddr;
  75.     Rect                     rectGW;
  76.     GWorldPtr                 pGWorld;
  77.     PixMapHandle             hPixMap;
  78.     unsigned long             pictRowBytes;
  79.     QDErr                    err;
  80.     GDHandle                oldGD;
  81.     GWorldPtr                oldGW;
  82.     short                    success = 1;
  83.     extern void                TellUser(Str255 s, OSErr e);
  84.     
  85.      
  86.     GetGWorld(&oldGW, &oldGD);     // save current port 
  87.                                 
  88.     SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
  89.     
  90.     // Important to preflight memory now, before asking for a GWorld.
  91.     if (TempFreeMem() < mapSizeX * mapSizeY * 4L + 1000)    {         
  92.         TellUser("\pNot enough memory to do this right now.", 0);
  93.         return 0;
  94.         }
  95.         
  96.     // Now create a 32-bit GWorld. 
  97.     err = NewGWorld(&pGWorld, 16, &rectGW, 0, 0, useTempMem);  
  98.     if (err != noErr)
  99.         return 0;
  100.     
  101.     // Get some basic info
  102.     hPixMap = GetGWorldPixMap(pGWorld);
  103.     pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
  104.     pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
  105.     
  106.     // Get ready to draw offscreen
  107.     SetGWorld(pGWorld, nil);
  108.     LockPixels(hPixMap);
  109.     EraseRect(&rectGW);
  110.     
  111.     // Draw it
  112.     DrawPicture(pict, &rectGW);
  113.             
  114.     // Finally, convert it to a storage pixmap
  115.     bMap->image = Q3MemoryStorage_New((const unsigned char *)pictMapAddr, 
  116.                                   pictRowBytes * mapSizeY);
  117.                                   
  118.     if (bMap->image == nil) {    // error  
  119.         success = 0;
  120.         goto bail;
  121.     }
  122.  
  123.     UnlockPixels(hPixMap);
  124.     
  125.     bMap->width     = mapSizeX;                // fill out the rest of the fields
  126.     bMap->height    = mapSizeY;
  127.     bMap->rowBytes     = pictRowBytes;
  128.     bMap->pixelSize = 16;
  129.     bMap->pixelType    = kQ3PixelTypeRGB16;
  130.     bMap->bitOrder    = kQ3EndianBig;
  131.     bMap->byteOrder    = kQ3EndianBig;
  132.      
  133. bail:
  134.     SetGWorld(oldGW, oldGD);                // restore the world
  135.     
  136.     DisposeGWorld(pGWorld);                    // kill the old world
  137.     
  138.     return success;
  139. }
  140.  
  141.  
  142.  
  143. // ------------------------------ IOErr() -------------------------------
  144. // Put up a basic Alert stating the error code.
  145. void IOErr( OSErr errcode ) 
  146. {
  147.     Str63            msg;
  148.     
  149.     if (!errcode) return;
  150.     NumToString( errcode, msg );
  151.     ParamText("\pAn I/O error occurred: ",msg,"\p","\p");
  152.     NoteAlert( IO_ALERT, 0 );
  153.  
  154. }